<!DOCTYPE HTML>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<meta name="viewport"
	content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title>用户注册</title>
		<style>
             body{
             	border-style: double;
             	width: 250px;
             	margin: 0 auto;
             	margin-top: 60px;
             	padding: 20px;
             }
             div{ 
             	text-align: center; 
             	padding: 5px 0;
             } 
             label{
             	text-align: right;
             	display: inline-block;
             	width: 100px;
             }
        </style>
	</head>
	<body>
		<div><h1>用户注册</h1></div>
		<div>
		<form>
			<div>
			<label for="username">新用户:</label>
			<input type="text" value="" id="username" name="username" maxlength="16" size="16">
			</div>
			<div>
			<label for="password">新密码:</label>
			<input type="password" value="" id="password" name="password" maxlength="16" size="16">
			</div>
			<div>
			<label for="repeatedpassword">重复密码:</label>
			<input type="password" value="" id="repeatedpassword" name="repeatedpassword" maxlength="16" size="16"> 
			</div>
			<div>
			<input type="button" onclick="checkregister()" value="注册">
			</div>
		</form>
		</div>
		<script>
			function checkregister(){
				var username = document.getElementById("username").value;
				var password = document.getElementById("password").value;
				var repeatedpassword = document.getElementById("repeatedpassword").value;
				var xhr = new XMLHttpRequest();

				var data = "username="+username+"&password="+password+"&repeatedpassword="+repeatedpassword;
				var url = 'recorduser.php';

				xhr.open("POST", url, true);
				xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				xhr.onreadystatechange = function(){
					if(xhr.readyState == 4 && xhr.status == 200){
						if(xhr.responseText == '1'){
							alert("注册成功!");
							window.location.href = "index.php";
						}
						else if(xhr.responseText == '2'){
							alert("密码与重复密码不一致!");
						}
						else if(xhr.responseText == '3'){
							alert('用户名已存在!');
						}
						else{
							alert('所有信息不准为空!');
						}
					}
				}
				xhr.send(data);
			}
		</script>
	</body>
</html>